package cn.globalph.housekeeper.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import cn.globalph.housekeeper.R
import cn.globalph.housekeeper.ui.dialog.LoadingViewDialog
import cn.globalph.housekeeper.ui.login.LoginActivity
import cn.globalph.housekeeper.utils.SystemUtils
import cn.globalph.housekeeper.utils.statusbar.StatusBarUtils
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.tool_bar.*

open class BaseFragment :Fragment(){
    open var isTopPicture = false
    open var barColor = R.color.colorPrimaryDark

    private lateinit var loadingDialog: LoadingViewDialog


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        loadingDialog = LoadingViewDialog()
        if(!isTopPicture){
//            StatusBarUtils.setImmersiveStatusBar(activity, false)
            StatusBarUtil.setTranslucentForImageViewInFragment(activity, 0, toolbar_layout)
            StatusBarUtil.setColor(activity, ContextCompat.getColor(context!!, barColor), 0)
        }else{
            StatusBarUtils.setImmersiveStatusBar(activity, true)
            StatusBarUtil.setTranslucentForImageViewInFragment(activity, 0, null)
        }
    }

    fun dp2px(dpVal: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal,
            resources.displayMetrics
        ).toInt()
    }

    fun TT(msg:String){
        SystemUtils.TT(context,msg)
    }

    fun tokenInvalid(){
        TT("用户信息失效，请重新登陆")
        Log.i("LoginData","startLogin")
        startActivity(Intent(context,LoginActivity::class.java))
        activity?.finish()
    }

    fun showLoading(){
        Log.i("loadinginfo",System.currentTimeMillis().toString())
        loadingDialog.show(fragmentManager!!,"loading")
    }

    fun dismissLoading(){
        Log.i("loadinginfo2",System.currentTimeMillis().toString())
        loadingDialog.dismiss()
    }
}